<?php

class NewsController extends CController
{
	const PAGE_SIZE=10;

	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction='list';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		
	}

	public function actionGet_news(){
		$model=$this->loadNews();
		$this->render('get_news',array('model'=>$model));
	}
	
	public function actionShow_news(){
		$criteria=new CDbCriteria();
		if(isset($_GET['kind']))
			$criteria->addCondition('kind='.$_GET['kind']);
		$model=News::model();
		$pages=new CPagination($model->count($criteria));
		$pages->pageSize=Yii::app()->params['page_size']['news_list'];
		$pages->applyLimit($criteria);
		$models=$model->findAll($criteria);
		$this->render('show_news',array(
			'models'=>$models,
			'pages'=>$pages,
			'kind'=>isset($_GET['kind'])?$model->getKindName($_GET['kind']):'全部'
		));
	}
	
	public function loadNews($id=null)
	{
		if($this->_model===null)
		{
			if($id!==null || isset($_GET['id']))
				$this->_model=News::model()->findbyPk($id!==null ? $id : $_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
}
